package com.example.himalaya.presenters;

import androidx.annotation.Nullable;

import com.example.himalaya.data.XimalayaApi;
import com.example.himalaya.interfaces.IAlbumDetailPresenter;
import com.example.himalaya.interfaces.IAlbumDetailViewCallback;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;

import java.util.ArrayList;
import java.util.List;

/**
 * 专辑详情业务逻辑接口实现类
 */
public class AlbumDetailPresenter implements IAlbumDetailPresenter {

    private static final String TAG = "AlbumDetailPresenter";
    private Album mTargetAlbum;

    private List<IAlbumDetailViewCallback> mAlbumDetailViewCallbacks = new ArrayList<>();
    private int mCurrentAlbumId =-1;
    private int mCurrentPage=1;
    private List<Track> mTracks = new ArrayList<>();

    private AlbumDetailPresenter() {
    }

    private static AlbumDetailPresenter sAlbumDetailPresenter=null;

    public static AlbumDetailPresenter getAlbumDetailPresenter() {
        if (sAlbumDetailPresenter == null) {
            synchronized (AlbumDetailPresenter.class) {
                if (sAlbumDetailPresenter == null) {
                    sAlbumDetailPresenter = new AlbumDetailPresenter();
                }
            }
        }
        return sAlbumDetailPresenter;
    }

    @Override
    public void pullDownToRefresh() {
    }

    @Override
    public void pullUpToLoadMore() {
        mCurrentPage++;
        doLoadedData(true);
    }

    /**
     *
     * @param isLoadMore 是否加载更多
     */
    private void doLoadedData(final boolean isLoadMore) {
        XimalayaApi ximalayaApi = XimalayaApi.getmXimalayaApi();
        ximalayaApi.getAlbumDetail(mCurrentAlbumId, mCurrentPage, new IDataCallBack<TrackList>() {
            @Override
            public void onSuccess(@Nullable TrackList trackList) {
                if (trackList != null) {
                    List<Track> tracks = trackList.getTracks();
                    if (isLoadMore) {
                        //下拉加载更多，结果放在后面
                        mTracks.addAll(tracks);
                        int size = tracks.size();
                        handlerLoadMoreResult(size);
                    } else {
                        mTracks.clear();
                        mTracks.addAll(tracks);
                    }
                    handlerAlbumDetailResult(mTracks);
                }

            }

            @Override
            public void onError(int i, String s) {
                if (isLoadMore) {
                    mCurrentPage--;
                }
                LogUtil.d(TAG, "错误码是 -->" + i);
                LogUtil.d(TAG, "错误信息是 -->" + s);
                handlerError();
            }
        });
    }

    /**
     * 处理程序加载更多结果
     * @param size
     */
    private void handlerLoadMoreResult(int size) {
        if (mAlbumDetailViewCallbacks != null) {
            for (IAlbumDetailViewCallback albumDetailViewCallback : mAlbumDetailViewCallbacks) {
                albumDetailViewCallback.onLoadMoreFinished(size);
            }
        }
    }

    @Override
    public void getDetailedTrackList(int albumId, int page) {
        this.mCurrentAlbumId = albumId;
        this.mCurrentPage = page;

        updateLoading();

        //根据专辑Id和页码,通过API请求，获得曲目列表
        doLoadedData(false);

    }

    private void updateLoading() {
        if (mAlbumDetailViewCallbacks != null) {
            for (IAlbumDetailViewCallback albumDetailViewCallback : mAlbumDetailViewCallbacks) {
                albumDetailViewCallback.onLoadLoading();
            }
        }
    }

    private void handlerError() {
        if (mAlbumDetailViewCallbacks != null) {
            for (IAlbumDetailViewCallback albumDetailViewCallback : mAlbumDetailViewCallbacks) {
                albumDetailViewCallback.onLoadNetworkError();
            }
        }
    }

    /**
     * 处理请求回来的专辑详情结果
     * @param tracks
     */
    private void handlerAlbumDetailResult(List<Track> tracks) {
        if (tracks != null) {
//            tracks.clear();//测试
            if (tracks.size() == 0) {
                if (mAlbumDetailViewCallbacks != null) {
                    for (IAlbumDetailViewCallback albumDetailViewCallback : mAlbumDetailViewCallbacks) {
                        albumDetailViewCallback.onLoadDataEmpty();
                    }
                }
            } else {
                if (mAlbumDetailViewCallbacks != null) {
                    for (IAlbumDetailViewCallback albumDetailViewCallback : mAlbumDetailViewCallbacks) {
                        albumDetailViewCallback.onLoadDetailedTrackList(tracks);
                    }
                }
            }
        }
    }

    @Override
    public void registerViewCallBack(IAlbumDetailViewCallback albumDetailViewCallback) {
        if (!mAlbumDetailViewCallbacks.contains(albumDetailViewCallback)) {
            mAlbumDetailViewCallbacks.add(albumDetailViewCallback);
            if (mTargetAlbum != null) {
                albumDetailViewCallback.onLoadAlbumInformation(mTargetAlbum);
            }
        }
    }

    @Override
    public void unRegisterViewCallBack(IAlbumDetailViewCallback albumDetailViewCallback) {
        if (mAlbumDetailViewCallbacks != null) {
            mAlbumDetailViewCallbacks.remove(albumDetailViewCallback);
        }
    }

    /**
     * 设置目标专辑
     * 将推荐界面被点击的专辑传送到专辑详情业务逻辑对象中
     * @param targetAlbum 被点击的专辑
     */
    public void setTargetAlbum(Album targetAlbum) {
        this.mTargetAlbum = targetAlbum;
    }
}
